#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


#define PATH "/dev/test_char_drv_01"
char read_buf[256] = {0};
char write_buf[256*10] = {0};

int main()
{
    int fd = 0;
	int len = 0;
	int i = 0;
	int pid = 0;
	

	fd = open(PATH, O_RDWR);
	if (fd < 0)
	{
		printf("open file %s error!\n", PATH);
		return -1;
	}

	pid = fork();

	if (pid == -1)
	{
		printf("fork error!\n");
		return -1;
	}

	if (pid == 0)
	{
		while (1)
		{
			memset(read_buf, 0, sizeof(read_buf));
			len = read(fd, read_buf, sizeof(read_buf));

			printf("child read len=%d\n", len);

			for(i=0; i<len; i++)
				printf("0x%x ", read_buf[i] & 0xFF);

			printf("\n");

			fflush(stdout);
		}		
		close(fd);
	}
	else
	{
		unsigned long long auto_add_cnt = 0;
		int loop_cnt = 100;

		while (--loop_cnt)
		{
			for (i=0; i<sizeof(write_buf); i++)
			{
				write_buf[i] = auto_add_cnt & 0xFF;
				auto_add_cnt++;
			}
			
			len = write(fd, write_buf, sizeof(write_buf));

			printf("write len=%d auto_add_cnt=%lld\n", len, auto_add_cnt);

			sleep(1);
		}
		close(fd);
	}
	
    return 0;
}
